package com.xgkj.diyiketang.fragment;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.data.DatiAdapter;
import com.xgkj.diyiketang.data.DatiInfo;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.task.GetDatiListTask;
import com.xgkj.diyiketang.task.GetUserDetailTask;
import com.xgkj.diyiketang.util.NoDataUtil;
import com.xgkj.diyiketang.weidget.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XCKJ_Paladowow on 2016/6/23.
 */
public class Fragment_DaTi extends BaseFragment implements PullToRefreshListView.OnRefreshListener, PullToRefreshListView.OnLoadDataListener {
    private Context mContext;
    private PullToRefreshListView datiListView;
    private DatiAdapter mAdapter;
    private int currentPage = 1;
    private int size = 20;
    private NoDataUtil noDataUtil;
    private View emptyView;
    private int detailTaskId = -1;
    private int userType = 0;
    private String cla;

    @Override
    protected int getContentViewId() {
        return 0;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_dati, null);
        mContext = getActivity();
        initView(view);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        mAdapter = new DatiAdapter(mContext);
        datiListView.setAdapter(mAdapter, true);
        datiListView.setOnRefreshListener(this);
        datiListView.setOnLoadDataListener(this);
        datiListView.doRefresh();
    }

    private void initView(View view) {
        datiListView = (PullToRefreshListView) view.findViewById(R.id.dati_listView);
        datiListView.init(mContext, true);
        getUserDetail();
//        datiListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                if (datiListView.getHeaderViewsCount() >0){
//                    position --;
//                }
//
//                if (position <0 || position>=mAdapter.getCount()) return;
//                DatiInfo info = (DatiInfo) mAdapter.getItem(position);
//                Intent detailIntent = new Intent(mContext, DatiDetailActivity.class);
//                detailIntent.putExtra("DATIINFO",info);
//                startActivity(detailIntent);
//            }
//        });
        emptyView = view.findViewById(R.id.content_layout);
        noDataUtil = new NoDataUtil(mContext, emptyView);
    }

    private void getDatiList() {
        if (currentPage == 0) {
            datiListView.setNoNextPage();
            return;
        }
        GetDatiListTask getDatiListTask = new GetDatiListTask(currentPage, size, null, null, null);
        getDatiListTask.addListener(new TaskListener(mContext));
        getEatShowApp().getTaskManager().addTask(getDatiListTask);
    }

    private void getUserDetail() {
        GetUserDetailTask task = new GetUserDetailTask();
        task.addListener(new MyListner(mContext));
        detailTaskId = getEatShowApp().getTaskManager().addTask(task);
    }

    class TaskListener extends ShowToastTaskEventListener {
        public TaskListener(Context context) {
            super(context);
        }

        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if (data != null) {
                List<DatiInfo> list = new ArrayList<DatiInfo>();
                list = (List<DatiInfo>) data;
                if (currentPage == 1) {
                    datiListView.onRefreshComplete();
                    datiListView.enableLoadMore();
                    mAdapter.clearData();
                } else {
                    datiListView.onLoadDataComplete(true);
                }

                mAdapter.updateData(list, userType);
                currentPage = result;
                Log.e("schoollist", "dati : result" + result);
                if (mAdapter != null && mAdapter.getCount() > 0) {
                    noDataUtil.ListViewNoEmpty(datiListView);
                } else {
                    noDataUtil.ListViewEmpty(datiListView, null, null, -1);
                }
            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if (currentPage == 1) {
                datiListView.onRefreshComplete();
            } else {
                datiListView.onLoadDataComplete(false);
            }
        }
    }

    @Override
    public void onPullRefresh() {

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            getUserDetail();
        }
    }

    @Override
    public void onLoadMore() {
        getDatiList();
    }

    @Override
    public void onRefresh() {
        currentPage = 1;
        getDatiList();
    }

    private class MyListner extends ShowToastTaskEventListener {
        public MyListner(Context context) {
            super(context);
        }

        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            EatShowUserInfo userInfo = (EatShowUserInfo) data;
            userType = userInfo.getUserType();
            cla = userInfo.getCla();
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }
    }
}
